package com.gxa.p2p.controller;
import com.gxa.p2p.businessExceptionCode.ErrorCode;
import com.gxa.p2p.exception.BusinessException;
import com.gxa.p2p.service.Userservice;
import com.gxa.p2p.service.impl.VerifyCodeServiceImpl;
import com.gxa.p2p.util.MapUtil;
import com.gxa.p2p.util.SendMessageUtil;
import io.swagger.annotations.ApiParam;
import io.swagger.models.auth.In;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;


// 开启跨域访问
@CrossOrigin
@Controller
public class SendVerifyCodeController {
    static Integer code22;
public static Integer getCode(){
    return code22;
}
    @Resource(name = "verifyCodeServiceImpl")
    VerifyCodeServiceImpl verifyCodeService;
    Userservice userservice;
    /**
     * 发送验证码
     * @param
     * @return
     */
    @RequestMapping("/sendVerifyCode")
    @ResponseBody
    public MapUtil sendVerifyCode(@RequestBody  @ApiParam Map<String,String> phonemap, HttpServletRequest request){
        //System.out.println(phonemap);

        String phoneNumber = phonemap.get("phoneNumber");
        System.out.println("接收到前端请求");
        MapUtil json = new MapUtil();
        try {
            verifyCodeService.sendVerifyCode(phoneNumber);
            Integer code = 222;
            code22 = 222;
           //Integer code = verifyCodeService.getCode();
          request.getSession().setAttribute("code1",code);
            json.setCode(0);
            json.setMsg("验证码发送成功");
        } catch (Exception e) {
            json.setCode(1);
            json.setMsg(e.getMessage());
        }

        return json ;



    }

    @ResponseBody
    @RequestMapping(value="compare" ,method = RequestMethod.POST)
    public Map <String, Object> compare(@RequestParam(value="code") Map <String, Object> mapcode){
        System.out.println(mapcode);
        Integer code1 = verifyCodeService.getCode();
        MapUtil mapUtil=new MapUtil();
            if((mapcode).equals(code1)){
                mapUtil.put("code", 0);
                mapUtil.put("msg", "验证正确可以继续操作");
            }else{
                throw new BusinessException(ErrorCode.FAILED);
            }



            return mapUtil.getMap();
    }

}
